﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Windows.Forms;

namespace PaliTextReader2.Utilities
{
    [Serializable]
    public class OpenBookInfo
    {
        int m_nFileID;
        int m_nScrollPos;

        public OpenBookInfo(int FileID,int ScrollPos)
        {
            m_nFileID = FileID;
            m_nScrollPos = ScrollPos;
        }

        public int FileID
        {
            get { return m_nFileID; }
            set { m_nFileID = value; }
        }

        public int ScrollPos
        {
            get { return m_nScrollPos; }
            set { m_nScrollPos = value; }
        }
    }

    [Serializable]
    public class BookmarkInfo
    {
        string m_strBookmarkName;
        int m_nFileID;
        int m_nScrollPos;

        public BookmarkInfo(string BookmarkName, int FileID, int ScrollPos)
        {
            m_strBookmarkName = BookmarkName;
            m_nFileID = FileID;
            m_nScrollPos = ScrollPos;      
        }

        public string BookmarkName
        {
            get { return m_strBookmarkName; }
            set { m_strBookmarkName = value; }
        }

        public int FileID
        {
            get { return m_nFileID; }
            set { m_nFileID = value; }
        }

        public int ScrollPos
        {
            get { return m_nScrollPos; }
            set { m_nScrollPos = value; }
        }
    }

    [Serializable]
    public class AppState
    {
        private List<OpenBookInfo> m_arrOpenBooks;
        private List<BookmarkInfo> m_arrBookmarks;
        private List<BookmarkInfo> m_arrRecentBooks;      
        private static string fileName = Application.UserAppDataPath+"\\app-state.dat";
        private static AppState appState;

        public static AppState Instance()
        {
            if (appState == null)
            {
                appState = new AppState();
            }
            return appState;
        }

        private AppState()
        {
            m_arrOpenBooks = new List<OpenBookInfo>();
            m_arrBookmarks=new List<BookmarkInfo>();
            m_arrRecentBooks = new List<BookmarkInfo>();
        }

        public void Deserialize()
        {
            if (File.Exists(fileName))
            {
                FileInfo info = new FileInfo(fileName);
                if (info.Length == 0L)
                {
                    File.Delete(fileName);
                }
                else
                {
                    FileStream serializationStream = new FileStream(fileName, FileMode.Open);
                    try
                    {
                        try
                        {
                            BinaryFormatter formatter = new BinaryFormatter();
                            appState = (AppState)formatter.Deserialize(serializationStream);

                            if (AppState.Instance().RecentBooks == null)
                                AppState.Instance().RecentBooks = new List<BookmarkInfo>();
                        }
                        catch (SerializationException exception)
                        {
                            Console.WriteLine("Failed to deserialize. Reason: " + exception.Message);
                            throw;
                        }
                    }
                    finally
                    {
                        serializationStream.Close();
                    }
                }
            }
        }

        public void Serialize()
        {
            FileStream serializationStream = new FileStream(fileName, FileMode.Create);
            BinaryFormatter formatter = new BinaryFormatter();
            try
            {
                try
                {
                    formatter.Serialize(serializationStream, appState);
                }
                catch (SerializationException exception)
                {
                    Console.WriteLine("Failed to serialize. Reason: " + exception.Message);
                    throw;
                }
            }
            finally
            {
                serializationStream.Close();
            }
        }

        public List<OpenBookInfo> OpenBooks
        {
            get { return m_arrOpenBooks; }
            set { m_arrOpenBooks = value; }
        }

        public List<BookmarkInfo> Bookmarks
        {
            get { return m_arrBookmarks; }
            set { m_arrBookmarks = value; }
        }

        public List<BookmarkInfo> RecentBooks
        {
            get { return m_arrRecentBooks; }
            set { m_arrRecentBooks = value; }
        }  
    }
}
